package com.gitee.zycra.union.api.common.param;

import com.gitee.zycra.union.api.common.model.ApiUser;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.experimental.Accessors;
import org.mindrot.jbcrypt.BCrypt;

import javax.validation.constraints.NotNull;

/**
 * @author zycra
 */
@Getter
@Setter
@ToString
@Accessors(chain = true)
public class AddUserParam {

    /**
     * 用户名
     */
    @NotNull(message = "用户名不能为空")
    private String username;

    /**
     * 密码
     */
    @NotNull(message = "密码不能为空")
    private String pwd;

    /**
     * 是否是超级管理员
     */
    private Boolean superAdmin;

    public static ApiUser convertToApiUser(AddUserParam addUserParam) {
        if (addUserParam == null) {
            return null;
        }
        return new ApiUser().setUsername(addUserParam.getUsername())
                .setSuperAdmin(addUserParam.getSuperAdmin())
                .setPwd(BCrypt.hashpw(addUserParam.getPwd(), BCrypt.gensalt()));
    }
}
